var $ = layui.$;
/**
 * zmn渠道-工单照片
 * @author linfeng
 * @since 2020/09/17 15:12
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [],
        page: true,
        limit: json.pageSize,
        height: 'full-149'
    });
    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    form.on("switch(chk_audit_flag)", function (obj) {
        let  url = "/channel/workorder/picture/updateAuditFlag.action";
        let id = obj.elem.getAttribute("key");
        let auditFlag = obj.elem.checked ? 2 : 1;
        switch (auditFlag) {
            case 1:
                zmnPage.ajaxSimple(url, {id: id, auditFlag: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {id: id, auditFlag: 2}, 1000);
                break;
        }
    });

    form.on("switch(chk_auto_audit_flag)", function (obj) {
        let  url = "/channel/workorder/picture/updateAutoAuditFlag.action";
        let id = obj.elem.getAttribute("key");
        let autoAuditFlag = obj.elem.checked ? 2 : 1;
        switch (autoAuditFlag) {
            case 1:
                zmnPage.ajaxSimple(url, {id: id, autoAuditFlag: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {id: id, autoAuditFlag: 2}, 1000);
                break;
        }
    });

    form.on("switch(chk_required_flag)", function (obj) {
        let  url = "/channel/workorder/picture/updateRequiredFlag.action";
        let id = obj.elem.getAttribute("key");
        let requiredFlag = obj.elem.checked ? 2 : 1;
        switch (requiredFlag) {
            case 1:
                zmnPage.ajaxSimple(url, {id: id, requiredFlag: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {id: id, requiredFlag: 2}, 1000);
                break;
        }
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        zmnPage.laySortPositiveInteger(obj.value,"/channel/workorder/picture/updateSort.action",obj.data.id,1000000,1000);
    });

    //对外暴露的接口
    exports('channel_workorder_picture_list', {});
});

function toAddWorkOrderPicture(channelId){
    layer.open({
        type: 2,
        title:'新增照片',
        area: ['600px', '550px'],
        content: '/channel/workorder/picture/addEditView.action?channelId='+channelId
    });

}

function toEditWorkOrderPicture(channelId, id){
    layer.open({
        type: 2,
        title:'修改照片',
        area: ['600px', '570px'],
        content: '/channel/workorder/picture/addEditView.action?channnelId='+channelId+"&id="+id
    });
}

function toAuditStandard(channelId, id){
    layer.open({
        type: 2,
        title: '设置审核标准',
        area: ['600px', '570px'],
        content: '/channel/workorder/picture/auditStandard.action?channnelId='+channelId+"&id="+id
    });
}

function deleteWorkOrderPicture(id) {
    layer.confirm('您确定要删除吗？', {
        btn: ['确定', '取消']
    }, function () {
        zmnPage.ajaxSimple("/channel/workorder/picture/delete.action", {id: id}, 1000);
    });
}

function reload(msg){
    parent.layui.layer.msg(msg);
    window.location.reload();
}

//显示描述
function show_tips(flag, tips, obj){
    //小tips
    //如果没有权限则提示
    if (!flag) {
        layer.tips(tips,obj,{
            tips:[1,'#3595CC'],
            time:3000
        })
    }
}

/**
 * 预览图片
 * @param element
 */
function previewImg(element) {
    //调整预览大小
    var img = new Image();
    img.src = $('#'+element).attr('src');
    var imgHtml = "<img src='" + img.src + "' width='100%' height='100%'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: [600 + 'px',300+'px'],
        shadeClose:true,
        scrollbar: false,
        title: "", //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}
